今天要來做的練習是關於 k8s 的網路操作
$ kubectl expose deployment/nginx --port=80
(on master node)$ kubectl describe po $(kubectl get po | grep nginx | awk -F' ' '{print $1}') | grep Node:
(on worker node)$ sudo tcpdump -i tunl0
(on master node)$ curl $(kubectl get svc | grep nginx | awk -F' ' '{print $3}')
$ kubectl delete svc nginx
$ kubectl expose deployment/nginx --port=80 --type=NodePort
$ kubectl get svc | grep nginx
(curl again)
first delete all resources created
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/baremetal/service-nodeport.yaml
modify /etc/hosts
(important)
$ echo '${WORKER1_IP} foo.bar.com' | sudo tee -a /etc/hosts
$ echo '${WORKER2_IP} foo.bar.com' | sudo tee -a /etc/hosts
cat.yml
kind: Pod
apiVersion: v1
metadata:
name: cat-app
labels:
app: cat
spec:
containers:
- name: cat-app
image: hashicorp/http-echo
args:
- "-text=cat"
---
kind: Service
apiVersion: v1
metadata:
name: cat-service
spec:
selector:
app: cat
ports:
- port: 5678
dog.yml
kind: Pod
apiVersion: v1
metadata:
name: dog-app
labels:
app: dog
spec:
containers:
- name: dog-app
image: hashicorp/http-echo
args:
- "-text=dog"
---
kind: Service
apiVersion: v1
metadata:
name: dog-service
spec:
selector:
app: dog
ports:
- port: 5678
ingress.yml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: default-ingress
namespace: default
annotations:
ingress.kubernetes.io/rewrite-target: /
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: foo.bar.com
http:
paths:
- path: /cat
backend:
serviceName: cat-service
servicePort: 5678
- path: /dog
backend:
serviceName: dog-service
servicePort: 5678